// pages/video/video.js
var utils = require('../../utils/util.js');

var requestPath = utils.requestPath;
var currentTime = utils.currentTime;
var startParams = utils.startParams;
var lengthParams = utils.lengthParams;
var topFlag = true;

Page({
  data: {
    newsObjs: []
  },
  onLoad: function(){
    var _this = this;
    GetdownList(_this);
  },
  onReady: function (res) {
    this.videoContext = wx.createVideoContext('myVideo')
  },
  onReachBottom: function () {
    // Do something when page reach bottom.
    GetupList(this);
  },
  onPullDownRefresh: function () {
    //下拉刷新
    wx.showNavigationBarLoading()
    startParams = 5;
    this.setData({
      newsObjs: [],
      tops: []
    });
    GetdownList(this)
  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
  },
  onShareAppMessage: function () {
    return {
      title: '我们临空港',
      desc: '我们临空港!'
    }
  },
  startPlay: function (e) {
    //点播功能
    var curVideoId = e.currentTarget.id;
    if (this.data.prevVideoId) {
      var prevV = wx.createVideoContext(this.data.prevVideoId);
      prevV.pause()
    }

    var videoContext = wx.createVideoContext(curVideoId);

    videoContext.play();
    this.setData({
      prevVideoId: curVideoId
    });
  }
})

// 上啦
var GetupList = function (that) {
  wx.request({
    url: requestPath + 'api/videoUpList',
    data: {
      releaseAt: currentTime,
      start: startParams,
      length: lengthParams
    },
    success: function (res) {
      var list = that.data.newsObjs;
      for (var i = 0; i < res.data.list.length; i++) {
        list.push(res.data.list[i]);
      }

      that.setData({
        newsObjs: list
      });
      startParams += lengthParams;
    }
  });
}

// 下拉刷新
var GetdownList = function (that) {
  wx.request({
    url: requestPath + 'api/videoDownList',
    data: {
      releaseAt: "2001-1-1 00:00:00"
    },
    success: function (res) {
      that.setData({
        newsObjs: res.data.list,
        topFlag: res.data.topFlag
      });
    },
    complete: function () {
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }
  });
}